package com.gmail.hongyz.myfuse.demo.action;

import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.gmail.hongyz.myfuse.common.Constants;
import com.gmail.hongyz.myfuse.common.PageObject;
import com.gmail.hongyz.myfuse.dataengine.Dataset;
import com.gmail.hongyz.myfuse.dataengine.JsonRequest;
import com.gmail.hongyz.myfuse.dataengine.JsonResponse;
import com.gmail.hongyz.myfuse.dataengine.Record;
import com.gmail.hongyz.myfuse.model.User;
import com.gmail.hongyz.myfuse.service.UserService;
import com.gmail.hongyz.myfuse.util.StringUtils;

public class DemoUserController extends MultiActionController {

	private static final Log log = LogFactory.getLog(DemoUserController.class);
	
	private UserService userService;
	
	public ModelAndView init(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		return new ModelAndView("user");
	}
	
	public void query(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		if (log.isDebugEnabled()) {
	        log.debug("entering 'handleRequest' method...");
	    }
		PageObject pageObject = PageObject.parseParams(request);
		List<User> users = userService.getUsers(pageObject);
		JSONObject result = new JSONObject();
		JSONArray list = JSONArray.fromObject(users);
		result.accumulate(Constants.EXT_PAGING_TOTAL_COUNT, pageObject.getTotal());
		result.accumulate(Constants.EXT_PAGING_CONTENT, list);
		response.getWriter().println(result.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	public void save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		
		String type = request.getParameter("type");
		String userId = request.getParameter("userId");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		log.debug("name: " + name);
		String email = request.getParameter("email");
		if ("new".equals(type)) {
			User user = new User();
			user.setUserId(userId);
			user.setName(name);
			user.setPassword(password);
			user.setEmail(email);
			log.info("new user: " + user);
			userService.saveUser(user);
		} else {
			User user = userService.getUserByUserId(userId);
			user.setName(name);
			user.setPassword(password);
			user.setEmail(email);
			log.info("edit user: " + user);
			userService.saveUser(user);
		}
		
//		JSONObject result = new JSONObject();
//		response.getWriter().println(result.toString());
//		response.getWriter().flush();
//		response.getWriter().close();
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void saveJson2(JsonRequest request, JsonResponse response) throws Exception {
		
		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		
		Dataset dataset = request.getDataset("userStore");
		log.info("dataset: " + dataset);
		for (int i = 0; i < dataset.size(); i++) {
			Record record = dataset.getRecord(i);
			log.info(record);
		}
		response.setParameter("saySomething", "hello,world");
	}
	
	public void callJson(JsonRequest request, JsonResponse response) throws Exception {
		
		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		
		response.setParameter("saySomething", "hello,world");
		
		PageObject pageObject = PageObject.parseParams(request.getServletRequest());
//		PageObject pageObject = PageObject.createPageObject();
		List<User> users = userService.getUsers(pageObject);
		
		Dataset dataset = Dataset.convertToDataset(users);
		dataset.setPageObject(pageObject);
		response.setDataset("userStore", dataset);
	}
}
